﻿using AutoMapper;
using BaseData.CommonLib.Models;
using BaseData.DomainLib.AggregatesModel.WorkstationAggregate;
using MediatR;
using Swimj.CommonLib.Base;

namespace BaseData.ApplicationLib.Commands;

public class CreateWorkstationCommand : WorkstationEditRequestDto, IRequest<Result<WorkstationInfoResponseDto>>
{
}

public class
    CreateWorkstationCommandHandler : IRequestHandler<CreateWorkstationCommand, Result<WorkstationInfoResponseDto>>
{
    private readonly IWorkstationRepository _workstationRepository;
    private readonly IMapper _mapper;

    public CreateWorkstationCommandHandler(IWorkstationRepository workstationRepository,
        IMapper mapper)
    {
        this._workstationRepository = workstationRepository;
        this._mapper = mapper;
    }

    public async Task<Result<WorkstationInfoResponseDto>> Handle(CreateWorkstationCommand request,
        CancellationToken cancellationToken)
    {
        var workstation = new Workstation(request.Code, request.Name, request.Description);
        _workstationRepository.Add(workstation);

        await _workstationRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

        return await Result<WorkstationInfoResponseDto>.SuccessAsync(_mapper.Map<WorkstationInfoResponseDto>(workstation));
    }
}